package cn.appsys.controller;

import cn.appsys.pojo.Info;
import cn.appsys.pojo.User;
import cn.appsys.service.InfoService;
import cn.appsys.service.UserService;
import cn.appsys.service.VersionService;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    UserService userService;
    @Resource
    InfoService infoService;
    @Resource
    VersionService versionService;

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "devlogin";
    }


    @RequestMapping("/dologin")
    public String login(@RequestParam(value = "devCode") String devCode, @RequestParam(value = "devPassword") String devPassword,
                        HttpServletRequest request) {
        User user = userService.Login(devCode, devPassword);
        if (user != null) {
            request.getSession().setAttribute("devUserSession", user);
            return "redirect:/user/toMain";
        } else {
            request.setAttribute("error", "用户名或密码不正确");
            return "devlogin";
        }
    }

    @RequestMapping("/toMain")
    public String toMain() {
        return "developer/main";
    }

    @RequestMapping("/loginOut")
    public String loginOut(HttpSession session) {
        session.invalidate();
        return "chooseLogin";
    }

    @RequestMapping("/toAdd")
    public String toAdd() {
        return "developer/appinfoadd";
    }

    @RequestMapping("/toAppList")
    public String appList() {
        return "developer/appinfolist";
    }

    @RequestMapping("/toUpdate")
    public String toCheck(@RequestParam("id") Long id, Model model) {
        model.addAttribute("appInfo", infoService.getInfoById(id));
        return "developer/appinfomodify";
    }

    @RequestMapping("/toVersoinAdd")
    public String toVersoinAdd(Long id, Model model) {
        Info infoById = infoService.getInfoById(id);
        model.addAttribute("appVersionList", versionService.getVersionListByAppId(id));
        model.addAttribute("appVersion", infoById);
        model.addAttribute("apkName", infoById.getApkName());
        return "developer/appversionadd";
    }

    @RequestMapping("/toVersoinUpdate")
    public String toVersoinUpdate(@RequestParam("vid") Long vid, @RequestParam("aid") Long aid, Model model) {
        Info infoById = infoService.getInfoById(aid);
        model.addAttribute("appVersionList", versionService.getVersionListByAppId(aid));
        model.addAttribute("appVersion", versionService.getVersionByAppId(aid));
        model.addAttribute("apkName", infoById.getApkName());
        return "developer/appversionmodify";
    }

    @RequestMapping("/toInfoView/{id}")
    public String toInfoView(@PathVariable("id") Long id, Model model) {
        model.addAttribute("appInfo", infoService.getInfoById(id));
        model.addAttribute("appVersionList", versionService.getVersionListByAppId(id));
        return "developer/appinfoview";
    }

    @RequestMapping("/deleteApp")
    @ResponseBody
    public Object deleteApp(@RequestParam("id") Long appId) {
        String str = "notexist";
        try {
            Info infoById = infoService.getInfoById(appId);
            if (infoById != null) {
                str = infoService.delInfo(appId,versionService.getVersionListByAppId(appId)) > 0 ? "true" : "false";
            }
        } catch (Exception e) {
            e.printStackTrace();
            str = "false";
        } finally {
            return JSON.toJSONString(str);
        }
    }

    @RequestMapping("/toChoose")
    public String toChoose() {
        return "chooseLogin";
    }

}
